
import React from 'react';
import { Button,Form } from 'antd';
import * as APIService from '../../axios/api_fileDown';
class BasicConfirm extends React.Component {
  state = {
    inputValue:'',
    disabledI:false
  };
  downFile=()=>{
    const p=this.props;
    this.setState({disabledI:true})
    APIService[`${p.apiPath}`](p.data).then(res=>{
      //设置下载文件类型为xlsx 不同的类型type也不一样，创建URL对象
      let url = window.URL.createObjectURL(new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }))
      // 创建A标签
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      // 设置的下载文件文件名
      const fileName = decodeURI(p.fileName);
      // 触发点击方法
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      this.setState({disabledI:false})
    }).catch((err)=>{
        this.setState({disabledI:false})
    })
  }

  render(){
    const p=this;
    return(
      <Button icon="arrow-down" onClick={()=>this.downFile()} type="primary" disabled={this.state.disabledI}>{!this.state.disabledI?p.props.buttonName:'正在导出'}</Button>
    )
  }
}


export default Form.create({})(BasicConfirm);
